package com.yyx.app1.MedicationAssistant_function.UI;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.yyx.app1.Data.DataInit;
import com.yyx.app1.Data.Medication.Medication_SQLiteOperation;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MedicationAssistant_function.Controls.Medication_Activity_Control.drup_listview.Medication_ListView_Adapter;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.ImageTool;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONObject;

/**
 * Created by yuexing on 2015/9/2.
 */
@EActivity(R.layout.activity_medication)
public class MedicationInfo_Activity extends Activity{

    @ViewById
    ListView medication_listview;

    @ViewById
    ImageView medication_alpha_top,medication_alpha_left,medication_alpha_bottom;

    @ViewById
    ImageView web_medication_image,web_medication_image_big;

    @ViewById
    LinearLayout medication_second,medication_name;

    @ViewById
    TextView medication_image_button,medication_medicinename;

    @AfterViews
    public void init()
    {
        try
        {
            medication_listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
            Intent intent = getIntent();
            String medicationName = intent.getStringExtra("medication");
            Medication_SQLiteOperation medication_sqLiteOperation =new Medication_SQLiteOperation(new DataInit(this,false,"cxlin"));
            JSONObject jsonObject = medication_sqLiteOperation.getByMedicineName(medicationName);
            Medication_ListView_Adapter adapter = new Medication_ListView_Adapter(this);
            adapter.findAll(jsonObject);
            medication_listview.setAdapter(adapter);
            Drawable drawable =new ColorDrawable(Color.WHITE);
            drawable.setAlpha(0);
            medication_listview.setDivider(drawable);
            medication_listview.setDividerHeight(5);
            medication_medicinename.setText(medicationName);
            //图片
            setImage(jsonObject.get("medicineImage").toString());
        }
        catch (Exception e)
        {
            Log.e("异常1510131637",e.toString());
        }
    }

    @Click({R.id.medication_alpha_top,R.id.medication_alpha_left,R.id.medication_alpha_bottom})
    public void gotogone()
    {
        medication_second.setVisibility(View.GONE);
    }

    @Click
    public void medication_name()
    {
        medication_second.setVisibility(View.VISIBLE);
    }

    @Click
    public void home()
    {
        finish();
    }

    @Click
    public void medication_image_button()
    {
        medication_second.setVisibility(View.VISIBLE);
    }

    public void setImage(String str)
    {
        try
        {
            if (str.isEmpty() || str == null)
            {
                web_medication_image_big.setImageResource(testDate.defaultMedicationImage);
                web_medication_image.setImageResource(testDate.defaultMedicationImage);
            }
            else {
                ImageLoader imageLoader = ((MyApp) getApplication()).getBaseDataManager().getM_imageLoader();
                imageLoader.get(str, imageLoader.getImageListener(web_medication_image, testDate.loadImageRes, testDate.defaultMedicationImage), ImageTool.dip2px(this, 45), ImageTool.dip2px(this, 45));
                imageLoader.get(str, imageLoader.getImageListener(web_medication_image_big, testDate.loadImageRes, testDate.defaultMedicationImage), ImageTool.dip2px(this, 150), ImageTool.dip2px(this, 150));
            }
        }
        catch (Exception e)
        {
            Log.e("异常1510131727",e.toString());
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            home();
        }
        return true;
    }
}
